//
//  ViewController.m
//  mac2
//
//  Created by 李劭华 on 2021/7/26.
//
#import <Masonry/Masonry.h>
#import "FirstViewController.h"
#import "secondViewController.h"
#import "ThirdViewController.h"
#import "fourViewController.h"

#import "ViewController.h"
@interface ViewController ()
@end

@implementation ViewController
- (BOOL)isFlipped{
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    [self setUpUI];
}
-(void)setUpUI
{

    NSRect frame = [[NSScreen mainScreen] visibleFrame];
    self.view.frame = CGRectMake(0, 0, 1000, 500);
    
  
    
    NSArray *btnTitleAry = @[@"nstableview 排序",@"nstableview 查找文件",@"拖拽换行",@"NSOutlineView",@"按钮5"];
    for (int i =0;  i< btnTitleAry.count; i++) {
        NSButton *btn = [NSButton buttonWithTitle:btnTitleAry[i] target:self action:@selector(touchBtnAction:)];
        btn.frame = CGRectMake((15+150)*i, 0, 150, 40);
        btn.tag = 100+i;
        [self.view addSubview:btn];
    }
    
}



#pragma mark - 点击事件
-(void)touchBtnAction:(NSButton *)btn
{
    NSLog(@"touchBtnAction--:%ld",(long)btn.tag);
    switch (btn.tag) {
        case 100:
        {
            FirstViewController *first = [[FirstViewController alloc]init];
            first.title =@"FirstViewController";
//            [self presentViewController:first asPopoverRelativeToRect:self.view.frame ofView:self.view preferredEdge:NSRectEdgeMaxY behavior:NSPopoverBehaviorSemitransient] ;

//            [self transitionFromViewController:self toViewController:first options:NSViewControllerTransitionCrossfade completionHandler:nil];
            
            first.rect = self.view.frame;
            [self presentViewControllerAsSheet:first];
        }
            break;
        case 101:
        {
            secondViewController *second = [[secondViewController alloc]init];
            second.rect = self.view.frame;
            [self presentViewControllerAsSheet:second];
        }
            break;
        case 102:
        {
            ThirdViewController *third = [[ThirdViewController alloc]init];
            third.rect = self.view.frame;
            [self presentViewControllerAsSheet:third];
        }
            break;
            
        case 103:{
            fourViewController *four = [[fourViewController alloc]init];
            four.rect = self.view.frame;
//            [self presentViewControllerAsSheet:four];
            [self presentViewControllerAsModalWindow:four];
        }
            break;
        case 104:{
            NSOpenPanel *openPanel = [NSOpenPanel openPanel];
                [openPanel setPrompt: @"打开"];
            openPanel.allowsMultipleSelection = YES;
            openPanel.canChooseDirectories = YES;
            openPanel.canChooseFiles = YES;
            openPanel.message = @"本应用需要访问该目录，请点击允许";
            openPanel.prompt =@"允许";
            openPanel.directoryURL = [[NSURL alloc]initFileURLWithPath:NSHomeDirectory()];
            openPanel.allowedFileTypes = [NSArray arrayWithObjects: @"txt", @"doc",@"png",@"pdf",@"JPG",@"app", nil];
            openPanel.directoryURL = nil;
            if ( NSModalResponseOK == [openPanel runModal] )
            {// 括号里要写的是下面的code
                NSArray *select_files = [openPanel URLs] ;
                if (select_files.count>0) {
                    
//                    [[NSWorkspace sharedWorkspace] openURL:select_files[0] configuration:nil completionHandler:^(NSRunningApplication * _Nullable app, NSError * _Nullable error) {
//                        NSLog(@"打开~");
//                    }];
//                    return;
                    NSURL *fileURL = select_files[0];
                    NSString *filePath = [fileURL path];
                    NSString *displayNameAtPath =[[NSFileManager defaultManager] displayNameAtPath:filePath];
                    NSLog(@"displayNameAtPath : %@",displayNameAtPath);
                    NSDictionary *attributesDic = [[NSFileManager defaultManager] attributesOfFileSystemForPath:filePath error:nil];
                    NSLog(@"attributesDic--:%@",attributesDic);
                    NSDictionary *attributesOfItemAtPathDic = [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil];
                    NSLog(@"attributesOfItemAtPathDic--:%@",attributesOfItemAtPathDic);


                    NSLog(@"isFilePackageAtPath : %d",[[NSWorkspace sharedWorkspace] isFilePackageAtPath:[fileURL path]]);
                    BOOL removable = NO;
                    BOOL writeable = NO;
                    BOOL unmountable = NO;
                    NSString *appName;
              
                    NSString *fileType = [[NSWorkspace sharedWorkspace] typeOfFile:[fileURL path] error:nil];
                    NSLog(@"fileType : %@",fileType);
                    NSString *desc = [[NSWorkspace sharedWorkspace] localizedDescriptionForType:fileType];
                    NSLog(@"desc : %@",desc);
                    NSLog(@"fileLabels : %@",[NSWorkspace sharedWorkspace].fileLabels);


                    
                    
                    NSImage *img =  [[NSWorkspace sharedWorkspace] iconForFile:[fileURL path]];
                    NSLog(@"img : %@",img);
                    
//                    "Use -[NSWorkspace URLForApplicationToOpenURL:] to get the URL of an application that will open a given item, or -[NSURL getResourceValue:forKey:error:] with NSURLContentTypeKey to get the type of the given item.
                    
//                    NSString *type1111;
//                    NSURL *theUrl = [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL:fileURL];
//                    [theUrl getResourceValue:&type1111 forKey:<#(nonnull NSURLResourceKey)#> error:<#(out NSError *__autoreleasing  _Nullable * _Nullable)#>];
                    
                    [[NSWorkspace sharedWorkspace] getInfoForFile: [fileURL path] application: &appName type: &fileType];
                    NSLog(@"appName : %@ , fileType : %@",appName,fileType);

                    NSString *description;
                    NSString *fileSystemType;
                    //用fullPath描述文件系统
                    [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath:[fileURL path]
                                           isRemovable: &removable
                                            isWritable: &writeable
                                         isUnmountable: &unmountable
                                           description: &description
                                                  type: &fileSystemType];
                   
                    NSString *fileInfo = [NSString stringWithFormat:
                                    @"AppName: %@\ntype: %@"
                                    @"\nremoveable: %d\nwriteable: %d\nunmountable: %d"
                                    @"\ndescription: %@\nfileSystemType: %@",
                                    appName, fileType,
                                    removable, writeable, unmountable,
                                    description, fileSystemType];
                   
                   NSLog (@" >> gather file info:\n%@", fileInfo);
   
                }
            }
            
//                [openPanel beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) {
//
//                    if (returnCode == 1) {
//                        NSLog(@"[openPanel URLs]--:%@",[openPanel URLs]);
//
//                        NSURL *fileUrl = [[openPanel URLs] objectAtIndex:0];
//                        // 获取文件内容
//                        NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:fileUrl error:nil];
//                        NSString *fileContext = [[NSString alloc] initWithData:fileHandle.readDataToEndOfFile encoding:NSUTF8StringEncoding];
//                        NSLog(@"fileContext--:%@",fileContext);
//                        // 将 获取的数据传递给 ViewController 的 TextView
////                        ViewController *mainViewController = (ViewController *)[self.view window].contentViewController;
////                        mainViewController.showCodeTextView.string = fileContext;
//                    }
//                }];
        }
            break;
        default:
            break;
    }
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


@end
